package me.sauce.folkpocket48.service

import android.app.Service
import android.content.Intent
import android.os.*
import android.widget.Toast
import me.sauce.folkpocket48.IMyAidlInterface
import me.sauce.folkpocket48.SCallback


class SimpleService : Service() {
    private val mBinder = object : IMyAidlInterface.Stub() {
        override fun registCallBack(callback: SCallback) {
            var handler = Handler();
            val runnable = object : Runnable {
                override fun run() {
                    callback.onUpdate(System.currentTimeMillis())
                    handler.postDelayed(this, 1)
                }
            }
            runnable.run()

        }

        val pid: Int
            get() = Process.myPid()

        override fun basicTypes(anInt: Int, aLong: Long, aBoolean: Boolean,
                                aFloat: Float, aDouble: Double, aString: String) {
            Toast.makeText(applicationContext, "anInt!", Toast.LENGTH_SHORT).show()

        }
    }

    override fun onBind(intent: Intent): IBinder? {

        return mBinder
    }

    companion object {
        val MSG_SAY_HELLO = 1
    }

    /**
     * Handler of incoming messages from clients.
     */
    internal inner class IncomingHandler : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                MSG_SAY_HELLO -> Toast.makeText(applicationContext, "hello!", Toast.LENGTH_SHORT).show()
                else -> super.handleMessage(msg)
            }
        }
    }


    internal inner class MBinder : Binder() {
        val service: SimpleService
            get() = this@SimpleService

    }

}
